package com.tyd.cams.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.tyd.cams.R;
import com.tyd.cams.entity.FanKabean;
import com.tyd.cams.utils.SPUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.strategy.impl.MaterialDialogStrategy;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * @author: TYD
 * @作者: 唐YaDong
 * @date: 2021/3/10 16:54
 * @description：FanKanRecyclerAdapter
 * @描述：FanKanRecyclerAdapter
 * @版本：1.0
 */

public class FanKanRecyclerAdapter extends RecyclerView.Adapter<FanKanRecyclerAdapter.ViewHplder> {
    Context context;
    List<FanKabean> fanKabeanList;
    LayoutInflater layoutInflater;
    private Integer zt;

    public FanKanRecyclerAdapter(Context context, List<FanKabean> fanKabeanList) {
        this.context = context;
        this.fanKabeanList = fanKabeanList;
//        this.layoutInflater = layoutInflater;
        DialogLoader mDialogLoader;

    }

    @Override
    public ViewHplder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.from(parent.getContext()).inflate(R.layout.foodcard_item_meun, parent
                , false);
        return new ViewHplder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHplder holder, int position) {
        FanKabean fanKabean = fanKabeanList.get(position);
        holder.card_name.setText("姓名：" + fanKabean.getPname());
        holder.card_num.setText(fanKabean.getCardNum());
        holder.card_yu_e.setText("余额:" + fanKabean.getYuE().toString());
        if (fanKabean.getType() == 1) {
            holder.lei_xin.setText("老师卡");
        } else {
            holder.lei_xin.setText(("学生卡"));
        }
        if (fanKabean.getCardStatus() == 1) {
            holder.card_ztai.setText("停用");
            holder.card_ty.setVisibility(View.GONE);
            holder.card_cz.setVisibility(View.VISIBLE);

        } else {
            holder.card_ztai.setText(" 正常");
            holder.card_cz.setVisibility(View.GONE);
            holder.card_ty.setVisibility(View.VISIBLE);

        }

    }

    @Override
    public int getItemCount() {
        return fanKabeanList == null ? 0 : fanKabeanList.size();
    }

    public class ViewHplder extends RecyclerView.ViewHolder {
        private TextView card_name;
        private TextView card_num;
        private TextView card_yu_e;
        private TextView lei_xin;
        private TextView card_ztai;
        private Button card_cz;
        private Button card_ty;
        private boolean zt = true;
        DialogLoader mDialogLoader = DialogLoader.getInstance().setIDialogStrategy(new MaterialDialogStrategy());

        private boolean setZtai(int i, String cardNum) {
            new Thread(() -> OkHttpUtils.post()
                    .url(SPUtils.getIP() + "/foodcard/msetztai")
                    .addHeader("Authorization", SPUtils.getToken())
                    .addParams("zt", String.valueOf(i))
                    .addParams("cardNum", cardNum)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                            zt = false;
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Gson son = new Gson();
                            HashMap<String, Object> map = new HashMap<>();
                            map = son.fromJson(response,HashMap.class);
                            Toast.makeText(context, "操作成功"+map.get("code"), Toast.LENGTH_SHORT).show();
                        }
                    })).start();

            return zt;
        }

        public ViewHplder(@NonNull View itemView) {
            super(itemView);
            card_name = itemView.findViewById(R.id.card_name);
            card_num = itemView.findViewById(R.id.card_num);
            card_yu_e = itemView.findViewById(R.id.card_yu_e);
            lei_xin = itemView.findViewById(R.id.lei_xin);
            card_ztai = itemView.findViewById(R.id.card_ztai);
            card_cz = itemView.findViewById(R.id.card_cz);
            card_ty = itemView.findViewById(R.id.card_ty);
            card_cz.setOnClickListener(v -> {
                mDialogLoader.showConfirmDialog(context, "是否启用饭卡", "确认",
                        (dialog, which) -> {
                            boolean b = setZtai(0, card_num.getText().toString());
                            if (b) {
                                Toast.makeText(context, "已经启用 " + card_num.getText().toString(), Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                                card_ztai.setText("状态：正常");
                                card_cz.setVisibility(View.GONE);
                                card_ty.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(context, "操作失败 " + card_num.getText().toString(), Toast.LENGTH_SHORT).show();
                            }

                        }
                        , "取消", (dialog, which) -> {
                            dialog.cancel();
                        });
                Log.i("启用", card_cz.getText().toString());

            });
            card_ty.setOnClickListener(v -> {
                mDialogLoader.showConfirmDialog(
                        context, "确认停用" + card_name.getText().toString() + "的饭卡",
                        "确认", (dialog, which) -> {
                            boolean b = setZtai(1, card_num.getText().toString());
                            if (b) {
                                dialog.dismiss();
                                Toast.makeText(context, "已经停用 " + card_num.getText().toString(), Toast.LENGTH_SHORT).show();
                                card_ztai.setText("状态：停用");
                                card_ty.setVisibility(View.GONE);
                                card_cz.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(context, "操作失败 " + card_num.getText().toString(), Toast.LENGTH_SHORT).show();
                            }

                        }, "取消", (dialog, which) -> {
                            dialog.cancel();
                        });
            });


        }
    }
}
